package com.hnu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnu.common.Constants;
import com.hnu.dto.Result;
import com.hnu.entity.Admin;
import com.hnu.entity.User;
import com.hnu.service.AdminService;
import com.hnu.service.UserService;
import com.hnu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author hj
 * @Date 2023/12/20 8:49
 * @PackageName:com.hnu.controller
 * @ClassName: AdminController
 * @Description: create a class
 * @Version 1.0
 */
@CrossOrigin //服务器端解决跨域
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //注册功能
    @PostMapping("/regs")
    public Result regs(@RequestBody User user) {
        //对密码进行加密
        user.setUserPassword(SecurityUtils.md5Hex(user.getUserPassword()));
        //设置创建时间
        //设置默认状态
        user.setUserStatus(1);
        user.setUserCreateTime(new Date());
        user.setUserUpdateTime(new Date());
        //调用服务层方法做添加操作
        boolean flang = userService.save(user);
        if (flang) {
            return Result.ok(Constants.ADD_SUCCESS).data(flang);
        }
        return Result.error(Constants.ADD_ERROR);
    }

    //登录功能
    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpSession session) {
        //对象密码进行加密处理
        String pass = SecurityUtils.md5Hex(user.getUserPassword());
        //根据用户名查询用户信息
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getUserName, user.getUserName());
        //使用adminService调用方法查询
        User user1 = userService.getOne(qw);
        if (user1 == null) {
            //帐号不存在
            return Result.error(Constants.LOGIN_UNKNOW_USERNAME);
        }
        if (!user1.getUserPassword().equals(pass)) {
            //密码错误
            return Result.error(Constants.LOGIN_ERROR_PASSWORD);
        }
        session.setAttribute("admin", user1);
        return Result.ok(Constants.LOGIN_OK).data(user1);
    }


    /**
     * 分页查询所有用户
     *
     * @param pageNum
     * @param pageSize
     * @return Result
     */
    @PostMapping("/findAll/{pageNum}/{pageSize}")
    public Result findAll(@PathVariable int pageNum,
                          @PathVariable int pageSize) {
        Page<User> page = userService.page(new Page<>(pageNum, pageSize));
        return Result.ok(Constants.OK).data(page);
    }

    @PostMapping("/status/{satuts}/{id}")
    public Result status(@PathVariable int satuts,
                         @PathVariable long id) {
        User user = userService.getById(id);
        if (user == null){
            return Result.ok(Constants.OK);
        }
        user.setUserStatus(satuts);
        user.setUserUpdateTime(new Date());
        userService.updateById(user);
        return Result.ok(Constants.OK);
    }

}
